package com.adk.backend.service.impl;

import com.adk.backend.common.PageResult;
import com.adk.backend.entity.LxClientList;
import com.adk.backend.mapper.ClientListMapper;
import com.adk.backend.service.ClientListService;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * 贸易客户列表服务实现类
 */
@Slf4j
@Service
public class ClientListServiceImpl extends ServiceImpl<ClientListMapper, LxClientList> implements ClientListService {
    
    @Autowired
    private ClientListMapper clientListMapper;
    
    @Override
    public PageResult<Map<String, Object>> getClientList(Map<String, Object> params) {
        Integer pageNum = (Integer) params.getOrDefault("pageNum", 1);
        Integer pageSize = (Integer) params.getOrDefault("pageSize", 20);
        
        // 计算总数
        Long total = clientListMapper.countClientList(params);
        
        // 设置分页参数
        params.put("offset", (pageNum - 1) * pageSize);
        params.put("limit", pageSize);
        
        // 查询列表
        List<Map<String, Object>> list = clientListMapper.selectClientListWithInfo(params);
        
        return PageResult.of(total, list, pageNum, pageSize);
    }
    
    @Override
    public Map<String, Object> getClientById(Integer id) {
        Map<String, Object> params = new HashMap<>();
        params.put("id", id);
        List<Map<String, Object>> list = clientListMapper.selectClientListWithInfo(params);
        if (list != null && !list.isEmpty()) {
            return list.get(0);
        }
        return null;
    }
    
    @Override
    @Transactional(rollbackFor = Exception.class)
    public LxClientList createClient(LxClientList clientList, Long userId) {
        int currentTime = (int) (System.currentTimeMillis() / 1000);
        clientList.setAddUid(userId != null ? userId.intValue() : 0);
        clientList.setAddTime(currentTime);
        
        clientListMapper.insert(clientList);
        return clientList;
    }
    
    @Override
    @Transactional(rollbackFor = Exception.class)
    public boolean updateClient(LxClientList clientList, Long userId) {
        int currentTime = (int) (System.currentTimeMillis() / 1000);
        clientList.setUpdateUid(userId != null ? userId.intValue() : 0);
        clientList.setUpdateTime(currentTime);
        
        return clientListMapper.updateById(clientList) > 0;
    }
    
    @Override
    @Transactional(rollbackFor = Exception.class)
    public boolean deleteClient(Integer id) {
        return clientListMapper.deleteById(id) > 0;
    }
    
    @Override
    public boolean checkClientExists(String ccName, String ccShort, Integer excludeId) {
        QueryWrapper<LxClientList> wrapper = new QueryWrapper<>();
        wrapper.and(w -> w.eq("cc_name", ccName).or().eq("cc_short", ccShort));
        if (excludeId != null && excludeId > 0) {
            wrapper.ne("id", excludeId);
        }
        return clientListMapper.selectCount(wrapper) > 0;
    }
}

